package com.woniu.his.mms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.mms.client.DrugManageClient;
import com.woniu.his.mms.client.param.AddDosagePram;
import com.woniu.his.mms.client.param.AddDrugPram;
import com.woniu.his.mms.client.param.FindDrugParam;
import com.woniu.his.mms.client.param.UpdateDrugParam;
import com.woniu.his.mms.client.result.DosageResult;
import com.woniu.his.mms.client.result.DrugResult;
import com.woniu.his.mms.client.result.PageDrugResult;
import com.woniu.his.mms.infra.model.Drug;
import com.woniu.his.mms.service.DrugService;
import com.woniu.his.mms.service.dto.DosageDto;
import com.woniu.his.mms.service.dto.PageDrugDto;
import com.woniu.his.mms.service.param.AddDosageServiceParam;
import com.woniu.his.mms.service.param.AddDrugServicePram;
import com.woniu.his.mms.service.param.FindDrugServiceParam;
import com.woniu.his.mms.service.param.UpdateDrugServiceParam;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/mms")
public class DrugManageApiImpl implements DrugManageClient {

    @Resource
    private DrugService drugService;

    @Override
    public Result addDrug(AddDrugPram param) {

        log.info("新增药品时的参数为：{}",param);
        AddDrugServicePram servicePram= BeanUtil.copyProperties(param,AddDrugServicePram.class);
        drugService.addDrug(servicePram);
        return Result.getInstance();
    }

    @Override
    public Result addDosage(AddDosagePram param) {

        log.info("新增剂型的参数:{}",param);
        AddDosageServiceParam param1=BeanUtil.copyProperties(param, AddDosageServiceParam.class);
        drugService.addDosage(param1);
        return Result.getInstance();
    }

    @Override
    public Result<List<DosageResult>> queryAllDosage() {

        log.info("查询所有剂型");
        List<DosageDto> list=drugService.queryAllDosage();
        List<DosageResult> results=BeanUtil.copyToList(list,DosageResult.class);
        return Result.getInstanceList(DosageResult.class).setData(results);
    }

    @Override
    public Result deleteDrugByIds(List<Long> ids) {

        log.info("传回来的药品ids：{}",ids);
        drugService.deleteDrugByIds(ids);
        return Result.getInstance();
    }

    @Override
    public Result<PageDrugResult> findDrug(@Valid FindDrugParam param) {

        log.info("参数为：{}",param);
        FindDrugServiceParam serviceParam=BeanUtil.copyProperties(param,FindDrugServiceParam.class);
        PageDrugDto page=drugService.findDrug(serviceParam);
        PageDrugResult result=BeanUtil.copyProperties(page,PageDrugResult.class);
        return Result.getInstance(PageDrugResult.class).setData(result);
    }

    @Override
    public Result updateDrug(UpdateDrugParam param) {

        log.info("传回来要修改的参数：{}",param);
        UpdateDrugServiceParam serviceParam=BeanUtil.copyProperties(param,UpdateDrugServiceParam.class);
        drugService.updateDrug(serviceParam);
        return Result.getInstance();
    }

    /**
     * 根据id查询药品
     * @param id
     * @return
     */
    @Override
    public Result<DrugResult> queryDrug(Long id) {
       log.info("根据id查询药品:{}", id);
        Drug byId = drugService.getById(id);
       if(null==byId) return null;
       if(byId.getStatusDrug().equals("ON")) return null;
       return Result.getInstance(DrugResult.class).setData(BeanUtil.copyProperties(byId,DrugResult.class));
    }
}
